﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<form id="form1">
    <div class="widget-body">
        @*<div id="wizard" class="wizard" data-target="#wizard-steps" style="border-left: none; border-top: none; border-right: none;">
        <ul class="steps">
            <li data-target="#step-1" class="active">相关信息<span class="chevron"></span></li>
            <li data-target="#step-2"><span class="step">2</span>功能权限 action="~/Treatment/AForm" method="post"<span class="chevron"></span></li>
        </ul>*@
    </div>
                @*<div class="alert alert-danger" style="text-align: left; margin-bottom: 10px;">
                    <i class="fa fa-warning alert-dismissible" style="position: relative; top: 1px; font-size: 15px; padding-right: 5px;"></i>
                    请填写相应信息！
                </div>*@
                <div class="panel panel-default">
                    @*<div class="panel-heading">
                        <h3 class="panel-title">表单信息</h3>
                    </div>*@
                    <div class="panel-bodys" style="width: 95%;padding-left:15px;">
                        <h2 id="sjxs" style="text-align:center"></h2>
                        <table class="form">
                            <tr>
                                <th class="formTitle">科室 ：</th>
                                <td class="formValue">
                                    <input type="hidden" class="form-control" id="ks" />
                                    <input type="text" class="form-control required" id="ksmz" />
                                </td>
                                <th class="formTitle">门诊类型：</th>
                                <td class="formValue">
                                    <select id="mjzbz" class="form-control required" data-EnumType="EnumOutPatientType">
                                        <option value="">请选择</option>
                                    </select>
                                    @*<select id="mjzbz" name="mjzbz" class="form-control required">
                                        <option value="">==请选择==</option>
                                        <option value="1">普通门诊</option>
                                        <option value="2">急诊</option>
                                        <option value="3">专家门诊</option>
                                        <option value="4">特病门诊</option>
                                        <option value="5">重大疾病门诊</option>
                                        <option value="6">慢性病门诊</option>
                                        <option value="7">居民两病</option>
                                        <option value="8">意外伤害门诊</option>
                                        <option value="9">生育门诊</option>
                                        <option value="10">耐多药结核门诊</option>
                                        <option value="11">儿童两病门诊</option>
                                    </select>*@
                                </td>
                                <th class="formTitle">排班医生：</th>
                                <td class="formValue"><input type="text" id="doctormc" name="doctormc" class="form-control form-an" /></td>
                                <th class="formTitle"></th>
                                <td class="formTitle">
                                    <input type="button" style="float:right" id="btn_search" class="btn btn-primary" value="查询" />
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="gridPanel">
                    <table id="gridList"></table>
                    <div id="gridPager"></div>
                </div>
        @*<div class="form-button" id="wizard-actions">
            <a id="btn_finish2" class="btn btn-default" onclick="AcceptClick()">完成</a>
            <a id="btn_last" disabled class="btn btn-default btn-prev">上一步</a>
            <a id="btn_next" class="btn btn-default btn-next">下一步</a>
            <a id="btn_finish" class="btn btn-default" style="display: none;" onclick="AcceptClick()">完成</a>
        </div>*@
</form>

<script>
    function getWeek(dateString) {
        var dateArray = dateString.split("-");
        date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
        return "周" + "日一二三四五六".charAt(date.getDay());
    };
    var sj = $.request("sj");
    var lx = $.request("lx");
    
    $(function () {
        $("#sjxs").text(sj);
        if (lx == 'pt') {
            $("#mjzbz option[value='1']").prop("selected", true).trigger('change');
        } else if (lx == 'zj') {
            $("#mjzbz option[value='3']").prop("selected", true).trigger('change');
        }
        GridInit();
    })
    //查询
    $('#btn_search').click(function () {
        getGriddata();
    });
    function getGriddata() {
        var ys = $("#doctormc").attr("data-label");
        if (ys == null || ys == "" || ys == "undefined" || $("#doctormc").val() == "") {
            ys = "";
        }

        var ks = $("#ksmz").val();
        if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
            ks = "";
        }
        $('#gridList').jqGrid("clearGridData");
        $('#gridList').jqGrid('setGridParam', {
            postData: { kssj: sj, jssj: sj, ys: ys, czztcx: '0', ScheduId: '', ks: ks, lx: $("#mjzbz").val() },
        }).trigger('reloadGrid');
    }
    function GridInit() {
        var ys = $("#doctormc").attr("data-label");
        if (ys == null || ys == "" || ys == "undefined") {
            ys = "";
        }
        var ks = $("#ksmz").val();
        if (ks == null || ks == "" || ks == "undefined") {
            ks = "";
        }
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/OutPatientManage/OutBook/GetSchedule",
            autowidth: true,
            postData: { kssj: sj, jssj: sj, ys: ys, czztcx: '0', ScheduId: '', ks: ks, lx: $("#mjzbz").val() },
            unwritten: false,
            height: $(window).height() - 180,
            colModel: [
                { label: "ScheduId", name: "ScheduId", hidden: true },
                { label: "编号", name: "ghpbId", width: 40, align: 'left', hidden: true },
                { label: "医生工号", name: "ysgh", width: 50, align: 'left', hidden: true },
                { label: "出诊科室", name: "czks", width: 80, align: 'left', hidden: true },
                { label: "出诊科室名称", name: "czksmc", width: 90, align: 'left' },
                { label: "医生姓名", name: "ysxm", width: 50, align: 'left' },
                {
                    label: "挂号类型", name: "RegType", width: 90, align: 'left', formatter: function (val) {
                        return $.enum.getDescByValue("EnumOutPatientType", val);
                        //if (val == "1") {
                        //    return "普通门诊";
                        //}
                        //else if (val == "2") {
                        //    return "急诊";
                        //} else if (val == "3") {
                        //    return "专家门诊";
                        //}
                        //else if (val == "4") {
                        //    return "特病门诊";
                        //}
                        //else if (val == "5") {
                        //    return "重大疾病门诊";
                        //}
                        //else if (val == "6") {
                        //    return "慢性病门诊";
                        //}
                        //else if (val == "7") {
                        //    return "居民两病";
                        //}
                        //else if (val == "8") {
                        //    return "意外伤害门诊";
                        //}
                        //else if (val == "9") {
                        //    return "生育门诊";
                        //}
                        //else if (val == "10") {
                        //    return "耐多药结核门诊";
                        //}
                        //else if (val == "11") {
                        //    return "儿童两病门诊";
                        //}
                    }
                },
                { label: "排班描述", name: "Title", width: 100, align: 'left' },
                { label: "出诊时间段", name: "PeriodDesc", width: 80, align: 'left' },
                { label: "yynum", name: "YYNum", width: 80, align: 'left', hidden: true },
                { label: "号源", name: "TotalNum", width: 60, align: 'left'},
                {
                    label: "剩余号源数", name: "action", width: 80, align: 'left', formatter: function (cellvalue, options, rowobject) {
                        return '&nbsp;<a style="color:red">'+(rowobject.TotalNum - rowobject.YYNum)+'<a>'
                    }
                },
                {
                    label: "是否已停诊", name: "action", width: 80, align: 'left',
                    formatter: function (cellvalue, options, rowobject) {
                        var str = "";
                        if (rowobject.istz == "1") {
                            str += '&nbsp;<a style="color:#ff0000">已停诊<a>';
                        } else {
                            str += '&nbsp;已出诊';
                        }
                        return str;
                    }
                },
                { label: "开始时间", name: "PeriodStart", width: 40, align: 'left', hidden: true },
                { label: "结束时间", name: "PeriodEnd", width: 40, align: 'left', hidden: true },
                { label: "挂号费", name: "RegFee", width: 60, align: 'left' },
                { label: "挂号项目", name: "sfxmmc", width: 80, align: 'left' },
                { label: "zt", name: "zt", width: 50, align: 'left', hidden: true },
            ],
            ondblClickRow: function (rowid) {
                AcceptClick(ScheduId);
            }
        });
    }
    function AcceptClick(callBack) {
        var ScheduId = $("#gridList").jqGridRowValue().ScheduId;
        if (!!!ScheduId) {
            $.modalAlert("请选中一条信息", 'warning');
            return false;
        }
        var ys = $("#ksmz").attr("data-label");
        if (ys == null || ys == "" || ys == "undefined") {
            ys = "";
        }
        callBack(ScheduId);
        $.modalClose();
    }
    $("#doctormc").newtouchFloatingSelector({
        height: 200,
        width: 260,
        clickautotrigger: true,
        filter: function (keyword) {
            //遍历数据源，用keyword来筛选出结果
            var resultObjArr = new Array();

            $.each(top.window.clients.sysStaffDutyList, function (idx, val) {
                if (((val.StaffPY && val.StaffPY.toLowerCase().indexOf(keyword.toLowerCase()) >= 0)
                    || (val.StaffName && val.StaffName.indexOf(keyword.toLowerCase()) >= 0)
                    || keyword.trim() == "")
                    && val.DutyCode == "Doctor") {
                    resultObjArr.push(val);
                }
            });
            return resultObjArr;

        },
        caption: "医生",
        colModel: [
            {
                label: '工号', name: 'StaffGh', widthratio: 25
            },
            {
                label: '名称', name: 'StaffName', widthratio: 25
            },
            { label: '拼音', name: 'StaffPY', widthratio: 25 }
        ],
        itemdbclickhandler: function ($thistr) {
            $("#doctormc").attr("data-label", $thistr.find("td:eq(0)").html());
            $("#doctormc").val($thistr.find('td:eq(1)').html());
            return;
        }
    });
    //科室
    $('#ksmz').newtouchFloatingSelector({
        width: 400,
        height: 180,
        caption: "科室",
        clickautotrigger: true,
        url: '/OutpatientManage/OutpatientRegSchedule/GetSysDepartmentList',
        ajaxmethod: 'POST',
        ajaxreqdata: function () {
            var reqData = {};
            reqData.keyword = $("#ksmz").val();
            return reqData;
        },
        itemdbclickhandler: function ($thistr) {
            $('#ksmz').attr('data-ks', $thistr.attr('data-code'))
                .val($thistr.attr('data-Name'));
            $('#ks').attr('data-ks', $thistr.attr('data-code'))
                .val($thistr.attr('data-code'));
            ////根据科室编号获取医生姓名
            //var ks = $thistr.attr('data-code');

            //$.ajax({
            //    url: "/OutPatientManage/OutBook/getStaffByKs?ks=" + ks,
            //    dataType: "json",
            //    async: false,
            //    success: function (data) {
            //        var staff = "";
            //        for (i = 0; i < data.length; i++) {
            //            staff += data[i] + ",";
            //        }
            //        if (staff.indexOf(",") != -1) {
            //            staff = staff.substring(0, staff.length - 1);
            //        }
            //        $("#ysxm").val(staff);
            //    }
            //});
        },
        colModel: [{ label: '代码', name: 'Code', widthratio: 30 },
        { label: '科室名称', name: 'Name', widthratio: 30 },
        { label: '拼音', name: 'py', widthratio: 30 }
        ]
    });
    function initControl() {
        //组织机构下拉框
        $("#OrganizeId").bindSelect({
            url: "/Organize/GetChildTreeSelectJson",
            selectedValue: $.request('orgId'),
        });

      
        $("#permissionTree").treeview({
            height: 400,
            slimscroll: false,
            showcheck: true,
            url: "/SystemManage/SysRoleAuthorize/GetPermissionTree",
            param: { roleId: keyValue }
        });
    }

    $("#zlxmmc").click(function () {
        $("#zlxmmc").val('');
    });
    
    var sfxmmc123 = "";
    var price123 = "";

    var zlxmpy123 = "";
    var sfdl = "";
    var zlxm123 = "";
    

    $('#zlxmmc').newtouchFloatingSelector({
        width: 600,
        height: 220,
        caption: "收费项目",
        clickautotrigger: true,
        url: '/SystemManage/BaseData/SelectSfxmYp',
        ajaxparameters: function () {
            return "keyword=" + $.trim($('#zlxmmc').val()) + "&mzzybz=1&dllb=2&sfdllx=OutpatientReg";
        },
        itemdbclickhandler: function ($thistr) {
            $('#zlxmmc').attr('data-ghlx', $thistr.attr('data-sfxmCode'))
                .attr('data-sfxmmc', $thistr.attr('data-sfxmmc'))
                .val($thistr.attr('data-sfxmmc'));
            $('#price').attr('data-ghlx', $thistr.attr('data-sfxmCode'))
                .attr('data-sfxmmc', $thistr.attr('data-sfxmmc'))
                .val($thistr.attr('data-dj'));
            $('#zlxmpy').attr('data-ghlx', $thistr.attr('data-sfxmCode'))
                .attr('data-sfxmmc', $thistr.attr('data-sfxmmc'))
                .val($thistr.attr('data-py'));

            sfxmmc123 = $thistr.attr('data-sfxmmc');
            price123 = $thistr.attr('data-dj');
            zlxmpy123 = $thistr.attr('data-py');

            sfdl = $thistr.attr('data-sfdlmc');
            zlxm123 = $thistr.attr('data-sfxmCode');
        },
        colModel: [{ label: '编码', name: 'sfxmCode', widthratio: 15 },
        { label: '名称', name: 'sfxmmc', widthratio: 25 },
        { label: '拼音', name: 'py', widthratio: 15 },
        { label: '单位', name: 'dw', widthratio: 8 },
        { label: '单价', name: 'dj', widthratio: 10 },
        { label: '收费大类', name: 'sfdlmc', widthratio: 15 },
        ]
    });
    



    function tianjiamx() {
        console.info("收费大类" + sfdl);
        var zlxmmcnull = $("#zlxmmc").val();
        if (!zlxmmcnull) {
            $.modalAlert("名称不能为空", 'warning');
        }
        else {

        
        if (!keyValue) {
            var btcode = $("#zhcode").val();
            var btzhmc = $("#zhmc").val();
            if (!btcode || !btzhmc) {
                $.modalAlert("名称和编码为必填项！", 'warning');
                return;
            }

            //判断文本框中的编码和明细表中的编码是否一致
            var jjzhcode = $("#MXTable").getCol("zhcode", false);
            
            var jjzhcodelie = $("#zhcode").val();
            var inzhcode = $.inArray(jjzhcode, chkzhcode)

           
            var count = $("#MXTable").getGridParam("reccount");
            
            if (count > 0) {
                if (jjzhcode != jjzhcodelie) {
                    $.modalAlert("两个编码不一样", 'warning');
                    addMX();
                    $("#MXTable").jqGrid("clearGridData");
                    
                }
            }
            var zhmc = $("#zhmc").val();
            var zhcode = $("#zhcode").val();
            var ord = $("#ord").val();
            //var zlxm = $("#zlxm").val();
            var datamx = [{ "zhmc": zhmc, "zhcode": zhcode, "ord": ord, "zlxm": zlxm123, "zlxmmc": sfxmmc123, "price": price123, "zlxmpy": zlxmpy123, "sfdl": sfdl }];
            //console.info(JSON.parse(data));
            var rowid = 0;


            var zhcodetj = $("#zhcode").val();
            
            //var id = $("#MXTable").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            //var rowData = $("#MXTable").jqGrid("getRowData", id);//根据上面的id获得本行的所有数据
            //var zhcodetj2 = rowData.zhcode; //获得制定列的值 （auditStatus 为colModel的name）
            //var zlxmmccx = rowData.zlxmmc;

            //获取某列数据
            var chkzhcode = $("#MXTable").getCol("zhcode", false);
            var inzhcode = $.inArray(zhcodetj, chkzhcode);
            var chkzlxmmc = $("#MXTable").getCol("zlxmmc", false);
            var inzlxmmc = $.inArray(sfxmmc123, chkzlxmmc);

            

            if (inzhcode = -1 && inzlxmmc == -1) {
                $.ajax({
                    url: "/Treatment/Tjchaxun",  //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,
                    data: { zhcodetj: zhcodetj, sfxmmc123: sfxmmc123 },  //参数值
                    type: "POST",   //请求方式
                    traditional: true,
                    success: function (data) {
                        
                        if (data = "0") {
                            $("#MXTable").addRowData(rowid, datamx, "last");
                        }
                        else {
                            $.modalAlert("该明细已存在",'success')
                        }

                        // $("#MXTable").addRowData(rowid, data, "last");
                        //请求成功时处理
                    },
                    error: function () {
                        //请求出错处理
                    }
                });
            }
            else {
                
                $.modalAlert("该明细已存在", 'success');
            }
        }
        else {
            
            $('#zhmc').attr("readonly", "readonly");
            $('#zhcode').attr("readonly", "readonly");
            
            var count = $("#MXTable").getGridParam("reccount");
            
            if (count > 0) {
                if (jjzhcode != jjzhcodelie) {
                    $.modalAlert("两个编码不一样", 'warning');
                    addMX();
                    $("#MXTable").jqGrid("clearGridData");

                }
            }
            
            var zhmc = $("#zhmc").val();
            var zhcode = $("#zhcode").val();
            var ord = $("#ord").val();
            //var zlxm = $("#zlxm").val();
            var datamx = [{ "zhmc": zhmc, "zhcode": zhcode, "ord": ord, "zlxm": zlxm123, "zlxmmc": sfxmmc123, "price": price123, "zlxmpy": zlxmpy123, "sfdl": sfdl }];
            console.info("sfdl" + JSON.stringify( datamx));
            //console.info(JSON.parse(data));
            var rowid = 0;
            
            var zhcodetj = $("#zhcode").val();

            //var id = $("#MXTable").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            //var rowData = $("#MXTable").jqGrid("getRowData", id);//根据上面的id获得本行的所有数据
            //var zhcodetj2 = rowData.zhcode; //获得制定列的值 （auditStatus 为colModel的name）
            //var zlxmmccx = rowData.zlxmmc;

            //获取某列数据
            var chkzhcode = $("#MXTable").getCol("zhcode", false);
            var inzhcode = $.inArray(zhcodetj, chkzhcode);
            var chkzlxmmc = $("#MXTable").getCol("zlxmmc", false);
            var inzlxmmc = $.inArray(sfxmmc123, chkzlxmmc);
            
            if (inzhcode = -1 && inzlxmmc == -1) {
                $.ajax({
                    url: "/Treatment/Tjchaxun",  //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,
                    data: { zhcodetj: zhcodetj, sfxmmc123: sfxmmc123 },  //参数值
                    type: "POST",   //请求方式
                    traditional: true,
                    success: function (data2) {
                        
                        if (data2 = "0") {
                            $("#MXTable").addRowData(rowid, datamx, "last");
                        }
                        else {
                            $.modalAlert("该明细已存在",'success')
                        }

                        // $("#MXTable").addRowData(rowid, data, "last");
                        //请求成功时处理
                    },
                    error: function () {
                        //请求出错处理
                    }
                });
            }
            else {

                $.modalAlert("该明细已存在",'success')
            }
        }
        }
        
        $("#zlxmmc").val('');

    }
    
    function deleteMX() {
        var keyValue = $.request("keyValue");
        console.info("获取keyvalue值"+keyValue)
        if (!keyValue) {
            var rowKey = $("#MXTable").jqGrid('getGridParam', "selrow");
            var rowid = $("#MXTable").getGridParam("selrow");
            $("#MXTable").delRowData(rowid);
        }
        else {
            var keyValue = $("#MXTable").jqGridRowValue().zlxmmc;
            var zhcodemc = $("#zhcode").val();
            console.info("这是获取的ID" + keyValue);
            if (!!!keyValue) {
                $.modalAlert("请选中一条信息", 'warning');
                return;
            }
            $.deleteForm({
                url: "/Treatment/deletemc",
                param: { keyValue: keyValue, zhcodemc: zhcodemc },
                success: function () {
                    //var rowKey = $("#MXTable").jqGrid('getGridParam', "selrow");
                    var rowid = $("#MXTable").getGridParam("selrow");
                    $("#MXTable").delRowData(rowid);
                    $.currentWindow().$("#MXTable").trigger("reloadGrid");
                }
            })
        }
        
    }
    
    function addBC() {



        var postData = $("#form1").formSerialize();
        postData["permissionIds"] = String($("#permissionTree").getCheckedNodes());

        //$.submitForm({
        //    param: postData,
        //    url: "/Treatment/tianjia?keyValue=" + keyValue,
        //    success: function () {

        //        $.currentWindow().$("#gridList").resetSelection();
        //        $.currentWindow().$("#gridList").trigger("reloadGrid");
        //        $.modalClose();
        //    }

        //})

        


        var postData = $("#form1").formSerialize();
        postData["permissionIds"] = String($("#permissionTree").getCheckedNodes());
        
        $.submitForm({
            param: postData,
            url: "/Treatment/tianjia?keyValue=" + keyValue,
            success: function () {
                $.currentWindow().$("#gridList").resetSelection();
                $.currentWindow().$("#gridList").trigger("reloadGrid");
                $.modalClose();
            }
            
        })
        
    }



    function addMX() {
        
        var rowData = $('#MXTable').jqGrid('getRowData');

        //var needRefundData = $("#gridList").jqGrid('getRowData_AllLine', true);
        //处理少一行bug 
        var allOrgId = $('#MXTable').jqGrid('getDataIDs'); //这里获取所有行 主键id 是全的
        rowData.push($('#MXTable').jqGrid('getRowData', allOrgId[allOrgId.length - 1]));
        
        

        var dataset = JSON.stringify(rowData);

        var lastdata = $("#MXTable").jqGrid('getRowData_AllLine');
        $.najax({
            url: "/Treatment/rowdata",
            dataType: "json",
            data: { dataset: dataset },
            traditional: true,
            type: "POST",
            success: function (data) {
                $.modalAlert("保存成功", 'success');
                $.currentWindow().$("#gridList").resetSelection();
                $.currentWindow().$("#gridList").trigger("reloadGrid");
                $.modalClose();
                
            }
        });
        

    }
    
    $("#MXTable").dataGrid({
        //url: "/Treatment/Login",
        //height: $(window).height() - 128,
        //postData: { keyword: $("#txt_keyword").val(), OrganizeId: $("#OrganizeId").val() },
        colModel: [
            //{ label: "主键", name: "Id", hidden: true, key: true },
            { label: '职称', name: 'zhcode', width: 80, align: 'left', hidden: true },
            {
                label: "挂号类型", name: "RegType", width: 60, align: 'left', formatter: function (val) {
                    if (val == "1") {
                        return "普通门诊";
                    }
                    else if (val == "2") {
                        return "急诊";
                    } else if (val == "3") {
                        return "专家门诊";
                    }
                    else if (val == "4") {
                        return "特病门诊";
                    }
                    else if (val == "5") {
                        return "重大疾病门诊";
                    }
                    else if (val == "6") {
                        return "慢性病门诊";
                    }
                    else if (val == "7") {
                        return "居民两病";
                    }
                    else if (val == "8") {
                        return "意外伤害门诊";
                    }
                    else if (val == "9") {
                        return "生育门诊";
                    }
                    else if (val == "10") {
                        return "耐多药结核门诊";
                    }
                    else if (val == "11") {
                        return "儿童两病门诊";
                    }
                }
            },
            { label: "医生工号", name: "ysgh", width: 50, align: 'left', hidden: true },
            { label: "医生姓名", name: "ysxm", width: 50, align: 'left' },
            { label: "出诊科室", name: "czks", width: 80, align: 'left', hidden: true },
            { label: "出诊科室名称", name: "czksmc", width: 80, align: 'left' },
            { label: "出诊时间段", name: "PeriodDesc", width: 80, align: 'left' },
            { label: '号源', name: 'TotalNum', width: 90, align: 'left', },
        ],
        width: "510",
        height: "150",
        pager: "#gridPager",
       // sortname: 'CreateTime desc',
        //viewrecords: true,
        rowNum: '10',
        unwritten: false,
    });
    
</script>
